package cdu.sl.service.impl;

import cdu.sl.mapper.ClazzMapper;
import cdu.sl.mapper.EmpMapper;
import cdu.sl.mapper.StudentMapper;
import cdu.sl.pojo.GenderOption;
import cdu.sl.pojo.JobOption;
import cdu.sl.pojo.StudentOption;
import cdu.sl.service.EmpService;
import cdu.sl.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public JobOption getEmpJobData() {
        // 1.调用mapper接口 , 获取统计数据
        // 这里由于查询到的结果是一个List集合 , 而这个List集合中装的元素是HashMap(员工职位(key) : 此职位的人数(value))
        // 可以通过下列语句进行检查
        System.out.println("查询员工职位信息的结果数据类型：" + empMapper.countEmpJobData().getClass());
        System.out.println("查询员工职位信息的结果的元素数据类型：" + empMapper.countEmpJobData().get(0).getClass());
        List<Map<String, Object>> list = empMapper.countEmpJobData();
        // 2.组装结果 , 并返回
        List<Object> jobList = list.stream().map(dataMap -> dataMap.get("pos")).toList();

        List<Object> dataList = list.stream().map(dataMap -> dataMap.get("num")).toList();

        return new JobOption(jobList, dataList);
    }

    /**
     * 统计员工职位信息
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getEmpGenderData() {
        // 1.直接获取结果并返回
        return empMapper.countEmpGenderData();
    }

    /**
     * 统计学生学历信息
     * @return
     */
    @Override
    public List<Map<String, Integer>> getStudentDegreeData() {
        return studentMapper.countStudentDegreeData();
    }

    /**
     * 统计班级人数信息
     * @return
     */
    @Override
    public StudentOption getStudentCountData() {
        // 1.获取到mapper层查询的结果
        List<Map<String, Object>> result=studentMapper.countStudentNumData();

        // 2.封装结果
        List<Object> clazzList=result.stream().map(dataMap->dataMap.get("clazz")).toList();

        List<Object> dataList=result.stream().map(dataMap->dataMap.get("val")).toList();

        return new StudentOption(clazzList,dataList);
    }
}
